package com.example.a2007dou_shop.login

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.a2007dou_shop.R
import com.example.a2007dou_shop.databinding.ActivityLoginCodeBinding
import com.example.a2007dou_shop.mvvm.BaseActivity
import com.example.a2007dou_shop.net.Cons

@Route(path = "/app/LoginCodeActivity")
class LoginCodeActivity : BaseActivity<ActivityLoginCodeBinding,LoginViewModel>() {
    override fun getLayout() = R.layout.activity_login_code

    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java

    override fun initView() {
        binding.getCode.setOnClickListener {
            viewModel.sendlcode(binding.etPhone.text.toString())
        }
        binding.btnLogincode.setOnClickListener {
            viewModel.logincode(binding.etPhone.text.toString(),binding.etCode.text.toString().toInt())
        }
        binding.goName.setOnClickListener {
            ARouter.getInstance().build("/app/LoginActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.sendSuccess.observe(this){
            if(it.code==0){
                binding.etCode.setText(it.data.toString())
            }
        }
        viewModel.logincodeSuccess.observe(this){
            if(it.code==0){
                SPUtils.getInstance().put(Cons.SP_TOKEN,it.data)
                ARouter.getInstance().build("/app/MainActivity").navigation()
            }
        }

    }

}